package com.party.web.web.dto.input.common;

import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CommonInput {

	// 时间类型
	private Integer timeType;

	// 创建时间——开始
	private String createStart;

	// 创建时间——结束
	private String createEnd;

	// 发布者
	private String memberName;

	//类型（0: 进行中 1:已截止 2：未开始）
	private Integer timeStatus;

	//积分类型
	private String cIntegralTypeId;

    //机构-会员ID
    private String pmId;

	//排行榜类型（0：行动力 1：影响力）
	private Integer leaderBoardType;

	private Integer limit; // 每页显示
	private String orderBy; // 排序

	private Integer isDate; // 1：不带时间 2：带时间

	//路线名称
	private String routeName;

	//会员昵称
	private String nickName;

	//会员手机号
	private String mobile;

	//机构id
	private String memberGroupId;

	private String partnerName;

    public String getPartnerName() {
        return partnerName;
    }

    public void setPartnerName(String partnerName) {
        this.partnerName = partnerName;
    }

    public String getMemberGroupId() {
		return memberGroupId;
	}

	public void setMemberGroupId(String memberGroupId) {
		this.memberGroupId = memberGroupId;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public CommonInput() {
		/*this.timeStatus = 0;*/
	}

	public String getRouteName() {
		return routeName;
	}

	public void setRouteName(String routeName) {
		this.routeName = routeName;
	}

	public String getPmId() {
        return pmId;
    }

    public void setPmId(String pmId) {
        this.pmId = pmId;
    }

    public String getcIntegralTypeId() {
		return cIntegralTypeId;
	}

	public void setcIntegralTypeId(String cIntegralTypeId) {
		this.cIntegralTypeId = cIntegralTypeId;
	}

	public Integer getLeaderBoardType() {
		return leaderBoardType;
	}

	public void setLeaderBoardType(Integer leaderBoardType) {
		this.leaderBoardType = leaderBoardType;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public Integer getTimeType() {
		return timeType;
	}

	public void setTimeType(Integer timeType) {
		this.timeType = timeType;
	}

	public String getCreateStart() {
		return createStart;
	}

	public void setCreateStart(String createStart) {
		this.createStart = createStart;
	}

	public String getCreateEnd() {
		return createEnd;
	}

	public void setCreateEnd(String createEnd) {
		this.createEnd = createEnd;
	}

	public String getMemberName() {
		return memberName;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	public Integer getTimeStatus() {
		return timeStatus;
	}

	public void setTimeStatus(Integer timeStatus) {
		this.timeStatus = timeStatus;
	}

	/**
	 * 拼接查询参数
	 * 
	 * @param input
	 * @return
	 */
	public static Map<String, Object> appendParams(CommonInput input) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (input == null) {
			input = new CommonInput();
		}

		if (StringUtils.isNotEmpty(input.getCreateStart()) || StringUtils.isNotEmpty(input.getCreateEnd())) {
			input.setTimeType(null);
		}

		/******** 时间块处理 ********/
		if (input.getTimeType() != null && input.getTimeType() != 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			Date ed = calendar.getTime(); // 结束时间
			if (input.getTimeType() == 2) { // 本周内
				int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
				if (day_of_week == 0) {
					day_of_week = 7;
				}
				calendar.add(Calendar.DATE, -day_of_week + 1);
			} else if (input.getTimeType() == 3) { // 本月内
				calendar.set(Calendar.DAY_OF_MONTH, 1);
			}
			Date sd = calendar.getTime(); // 开始时间
			String std = sdf.format(sd) + " 00:00:00";
			params.put("startDate", std);
			String end = sdf.format(ed) + " 23:59:59";
			params.put("endDate", end);
		}
		/******** 时间块处理 ********/

		/******** 时间段处理 ********/
		if (StringUtils.isNotEmpty(input.getCreateStart())) {
			if (new Integer(1).equals(input.getIsDate())) {
				params.put("c_start", input.getCreateStart() + " 00:00:00");
			} else {
				params.put("c_start", input.getCreateStart());
			}
		}
		if (StringUtils.isNotEmpty(input.getCreateEnd())) {
			if (new Integer(1).equals(input.getIsDate())) {
				params.put("c_end", input.getCreateEnd() + " 23:59:59");
			} else {
				params.put("c_end", input.getCreateEnd());
			}
		}
		/******** 时间段处理 ********/

		/******** 发布者处理 ********/
		if (StringUtils.isNotEmpty(input.getMemberName())) {
			params.put("memberName", input.getMemberName());
		}
		/******** 发布者处理 ********/
		if(null != input.getTimeStatus()){
			params.put("timeStatus", input.getTimeStatus());
		}

		if (null != params.get("startDate")) {
			input.setCreateStart(params.get("startDate").toString().substring(0, "2018-05-04".length()));
		}
		if (null != params.get("endDate")) {
			input.setCreateEnd(params.get("endDate").toString().substring(0, "2018-05-04".length()));
		}
		return params;
	}

	/***
	* 处理排行榜查询参数
	* @param input
	* @return
	**/
	public static Map<String, Object> appendParams1(CommonInput input) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (input == null) {
			input = new CommonInput();
		}

		if (StringUtils.isNotEmpty(input.getCreateStart()) || StringUtils.isNotEmpty(input.getCreateEnd())) {
			input.setTimeType(null);
		}

		/******** 时间块处理 ********/
		if (input.getTimeType() != null && input.getTimeType() != 0 && input.getTimeType() != 4) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			Date ed = calendar.getTime(); // 结束时间
			if (input.getTimeType() == 1) { // 本月内
				calendar.set(Calendar.DAY_OF_MONTH, 1);
			} else if (input.getTimeType() == 2) { // 本季度
				Date currentQuarterStartTime = getCurrentQuarterStartTime(calendar);
				calendar.setTime(currentQuarterStartTime);
			} else if (input.getTimeType() == 3)  //本年
			{
				calendar.set(Calendar.DAY_OF_YEAR,1);
			}
			Date sd = calendar.getTime(); // 开始时间
			String std = sdf.format(sd) + " 00:00:00";
			params.put("startDate", std);
			String end = sdf.format(ed) + " 23:59:59";
			params.put("endDate", end);
		}
		/******** 时间块处理 ********/

		/******** 时间段处理 ********/
		if (StringUtils.isNotEmpty(input.getCreateStart())) {
			if (new Integer(1).equals(input.getIsDate())) {
				params.put("c_start", input.getCreateStart() + " 00:00:00");
			} else {
				params.put("c_start", input.getCreateStart());
			}
		}
		if (StringUtils.isNotEmpty(input.getCreateEnd())) {
			if (new Integer(1).equals(input.getIsDate())) {
				params.put("c_end", input.getCreateEnd() + " 23:59:59");
			} else {
				params.put("c_end", input.getCreateEnd());
			}
		}
		/******** 时间段处理 ********/

		return params;
	}

	/***
	* 获取当前月份的季度开始时间
	* @Date 17:33 2019/10/12
	* @param c
	* @return
	**/
	public static  Date getCurrentQuarterStartTime(Calendar c)
	{
		SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		int currentMonth = c.get(Calendar.MONTH) + 1;

		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 3)
				c.set(Calendar.MONTH, 0);
			else if (currentMonth >= 4 && currentMonth <= 6)
				c.set(Calendar.MONTH, 3);
			else if (currentMonth >= 7 && currentMonth <= 9)
				c.set(Calendar.MONTH, 4);
			else if (currentMonth >= 10 && currentMonth <= 12)
				c.set(Calendar.MONTH, 9);
			c.set(Calendar.DATE, 1);
			now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;

	}

	public static Map<String, Object> appendPageParams(Page page) {
		Map<String, Object> params = Maps.newHashMap();
		int offset = (page.getPage() - 1) * page.getLimit();
		params.put("offset", offset);
		params.put("limit", page.getLimit());
		return params;
	}

	public Integer getIsDate() {
		return isDate;
	}

	public void setIsDate(Integer isDate) {
		if (null == isDate) {
			isDate = 2;
		}
		this.isDate = isDate;
	}

	/**
	 * 最近7天，30天
	 *
	 * @param commonInput
	 */
	public static void transfromDate(CommonInput commonInput) {
		String startTime = commonInput.getCreateStart();
		String endTime = commonInput.getCreateEnd();
		if (commonInput.getTimeType() == null && StringUtils.isEmpty(commonInput.getCreateStart())
				&& StringUtils.isEmpty(commonInput.getCreateEnd())) {
			commonInput.setTimeType(1);
		}
		if (commonInput.getTimeType() != null) {
			String now = DateUtils.todayDate();
			endTime = DateUtils.todayDate();
			if (commonInput.getTimeType().equals(1)) {
				startTime = now;
			} else if (commonInput.getTimeType().equals(2)) {
				startTime = DateUtils.formatDate(DateUtils.addDay(now, -1));
				endTime = DateUtils.formatDate(DateUtils.addDay(now, -1));
			} else if (commonInput.getTimeType().equals(3)) {
				startTime = DateUtils.formatDate(DateUtils.addDay(now, -6));
			} else if (commonInput.getTimeType().equals(4)) {
				startTime = DateUtils.formatDate(DateUtils.addDay(now, -29));
			}
		}
		commonInput.setCreateStart(startTime);
		commonInput.setCreateEnd(endTime);
	}
}
